package info.wangyuan.agent.initializer;

import cn.hutool.core.io.FileUtil;
import info.wangyuan.agent.utils.RagRebuildService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


/**
 * 默认数据初始化器 在应用启动时自动初始化默认用户数据
 * 设置靠后优先级为100，确保在其他初始化器之后执行
 *
 * @author Albert
 * @since 2025-07-17 01:24:50
 */
@Slf4j
@Component
@Order(100)
public class DefaultDataInitializer implements ApplicationRunner {

    private final RagRebuildService ragDataSetService;

    public DefaultDataInitializer(RagRebuildService ragDataSetService) {
        this.ragDataSetService = ragDataSetService;
    }

    @Override
    public void run(ApplicationArguments args) {
        log.info("开始初始化WYAgent默认数据...");
        try {
//            createDirIfAbsent(FileConstant.LOG_ROOT_PATH, "日志目录");
//            createDirIfAbsent(FileConstant.RAG_ROOT_PATH, "知识库目录");
//            createDirIfAbsent(FileConstant.STATIC_ROOT_PATH, "静态文件目录");
//            ragDataSetService.rebuild(FileConstant.RAG_ROOT_PATH);
            log.info("WYAgent默认数据初始化完成！");
        } catch (Exception e) {
            log.error("WYAgent默认数据初始化失败", e);
            // 不抛出异常，避免影响应用启动
        }
    }

    /**
     * 若目录不存在则创建
     */
    private void createDirIfAbsent(String path, String desc) {
        if (!FileUtil.exist(path)) {
            FileUtil.mkdir(path);
            log.info("已创建默认 {}", desc);
        }
    }

}
